package dongchel.vrp.ais;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dongchel.vrp.entity.MultiObjectiveObject;
import dongchel.vrp.entity.Solution;
import dongchel.vrp.general.CompareObject;
import dongchel.vrp.general.MultiObjectiveComparator;


public class Fitness {
	private List<Solution> population;	
	private List<Solution> sortedPopulation;
	
	private List<List> hierarchy;
	
	
	public Fitness(List<Solution> population){
		this.population = population;
	}
	
	public void sortPopulation(){
		List<CompareObject> compareObjectList = new ArrayList<CompareObject>();
		for(int i=0;i<population.size();i++){
			CompareObject compareObject = new CompareObject();
			compareObject.setObject(population.get(i));
			compareObject.setValue(population.get(i).getObjectiveValue());
			compareObjectList.add(compareObject);
		}
		
		
		Collections.sort(compareObjectList,new MultiObjectiveComparator());
	
			for(int j=0;j<compareObjectList.size()-1;j++){
				MultiObjectiveObject currentElement = (MultiObjectiveObject)compareObjectList.get(j).getValue();
				MultiObjectiveObject nextElement = (MultiObjectiveObject)compareObjectList.get(j+1).getValue();
				//System.out.println("o1:"+currentElement.getObjectiveList().get(0)+"  o2:"+currentElement.getObjectiveList().get(1));
				//System.out.println("comparator:"+currentElement.compare(nextElement));
			}

		
		hierarchy = new ArrayList();
		List<CompareObject> layer = new ArrayList<CompareObject>();
		MultiObjectiveObject lastElement = (MultiObjectiveObject)compareObjectList.get(0).getValue();
		layer.add(compareObjectList.get(0));
		for(int i=1;i<compareObjectList.size();i++){
			MultiObjectiveObject currentElement = (MultiObjectiveObject)compareObjectList.get(i).getValue();
			
			
			if(lastElement.compare(currentElement)!=0){
				
				hierarchy.add(layer);
				layer = new ArrayList<CompareObject>();		
			}
			layer.add(compareObjectList.get(i));
			lastElement = currentElement;
		}
		
		for(int i=0;i<hierarchy.size();i++){
			layer = hierarchy.get(i);
			//System.out.println("layer :"+i);
			for(int j=0;j<hierarchy.get(i).size();j++){
				MultiObjectiveObject currentElement = (MultiObjectiveObject)((CompareObject)(hierarchy.get(i).get(j))).getValue();
				//System.out.println("o1:"+currentElement.getObjectiveList().get(0)+"  o2:"+currentElement.getObjectiveList().get(1));
			}
			//System.out.println("**************");
		}
	}

	public List<Solution> getPopulation() {
		return population;
	}

	public void setPopulation(List<Solution> population) {
		this.population = population;
	}

	public List<Solution> getSortedPopulation() {
		return sortedPopulation;
	}

	public void setSortedPopulation(List<Solution> sortedPopulation) {
		this.sortedPopulation = sortedPopulation;
	}

	public List<List> getHierarchy() {
		return hierarchy;
	}

	public void setHierarchy(List<List> hierarchy) {
		this.hierarchy = hierarchy;
	}
	
	
}
